package com.carleasoft.mps.cms.business.message.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.message.bean.entity.MessageLeading;
import com.carleasoft.mps.cms.business.message.bean.po.MessageLeadingPageQueryPo;
import com.carleasoft.mps.cms.business.message.bean.vo.MessageLeadingDetailVo;
import com.carleasoft.mps.cms.business.message.bean.vo.MessageLeadingVo;
import com.carleasoft.mps.cms.business.message.dao.MessageLeadingMapper;
import com.carleasoft.mps.cms.business.message.service.MessageLeadingService;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 信箱首长科室信息表 服务实现类
 * </p>
 *
 * @author fan
 * @since 2023-06-09
 */
@Service
public class MessageLeadingServiceImpl extends ServiceImpl<MessageLeadingMapper, MessageLeading> implements MessageLeadingService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(MessageLeading messageLeading) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        messageLeading.setBusinessCode(UUID.randomUUID().toString().replaceAll("-",""));
        messageLeading.setCreatePersonCode(user.getUserCode());
        messageLeading.setCreatePersonName(user.getUserName());
        messageLeading.setCreateTime(LocalDateTime.now());
        messageLeading.setUpdatePersonCode(user.getUserCode());
        messageLeading.setUpdatePersonName(user.getUserName());
        messageLeading.setUpdateTime(LocalDateTime.now());
        messageLeading.setIsDelete(0);
        messageLeading.setStatus(1);//默认启用
        return this.add(messageLeading);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(MessageLeading messageLeading) {
        IBeanUtil.paramVerify(messageLeading,"businessCode");
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        messageLeading.setCreateTime(LocalDateTime.now());
        messageLeading.setUpdatePersonCode(user.getUserCode());
        messageLeading.setUpdatePersonName(user.getUserName());
        messageLeading.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<MessageLeading> eq = Wrappers.lambdaQuery(MessageLeading.class).eq(MessageLeading::getBusinessCode, messageLeading.getBusinessCode());
        return this.update(messageLeading,eq);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(MessageLeading messageLeading) {
        IBeanUtil.paramVerify(messageLeading,"businessCode");
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        LambdaUpdateWrapper<MessageLeading> updateWrapper = Wrappers.lambdaUpdate(MessageLeading.class).eq(MessageLeading::getBusinessCode, messageLeading.getBusinessCode()).set(MessageLeading::getIsDelete, 1);
        updateWrapper.set(MessageLeading::getUpdatePersonCode,user.getUserCode());
        updateWrapper.set(MessageLeading::getUpdatePersonName,user.getUserName());
        updateWrapper.set(MessageLeading::getUpdateTime,LocalDateTime.now());
        return this.update(updateWrapper);
    }

    @Override
    public Page<MessageLeadingVo> pageQuery(MessageLeadingPageQueryPo messageLeadingPageQueryPo) {
        Page page = new Page(messageLeadingPageQueryPo.getCurrent(),messageLeadingPageQueryPo.getSize());
        return this.baseMapper.pageQuery(page,messageLeadingPageQueryPo);
    }

    @Override
    public MessageLeadingDetailVo detail(MessageLeading messageLeading) {
        return this.baseMapper.detail(messageLeading.getBusinessCode());
    }

    @Override
    public List<MessageLeadingVo> selectList() {
        return this.baseMapper.selectList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean stopOrStart(MessageLeading messageLeading) {
        IBeanUtil.paramVerify(messageLeading,"businessCode");
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        LambdaUpdateWrapper<MessageLeading> updateWrapper = Wrappers.lambdaUpdate(MessageLeading.class).eq(MessageLeading::getBusinessCode, messageLeading.getBusinessCode()).set(MessageLeading::getStatus, messageLeading.getStatus());
        updateWrapper.set(MessageLeading::getUpdatePersonCode,user.getUserCode());
        updateWrapper.set(MessageLeading::getUpdatePersonName,user.getUserName());
        updateWrapper.set(MessageLeading::getUpdateTime,LocalDateTime.now());
        return this.update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean editSort(MessageLeading messageLeading) {
        IBeanUtil.paramVerify(messageLeading,"businessCode");
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        LambdaUpdateWrapper<MessageLeading> updateWrapper = Wrappers.lambdaUpdate(MessageLeading.class).eq(MessageLeading::getBusinessCode, messageLeading.getBusinessCode()).set(MessageLeading::getTheSort, messageLeading.getTheSort());
        updateWrapper.set(MessageLeading::getUpdatePersonCode,user.getUserCode());
        updateWrapper.set(MessageLeading::getUpdatePersonName,user.getUserName());
        updateWrapper.set(MessageLeading::getUpdateTime,LocalDateTime.now());
        return this.update(updateWrapper);
    }
}
